package org.example.exception;

import com.aliyun.oss.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.example.pojo.Result;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler{
    @ExceptionHandler
    public Result defaultExceptionHandler(Exception e){
        log.error("出错了",e);
        return Result.error("出错了");
    }
    @ExceptionHandler
    public Result handleDuplicateKeyException(DuplicateKeyException e){
        log.error("出错了",e);
        String msg = e.getMessage();
        int index = msg.indexOf("Duplicate entry");
        String errorMsg = msg.substring(index);
        String[] split = errorMsg.split(" ");
        return Result.error(split[2]+"已存在");

    }

    @ExceptionHandler
    public Result handleClazzHasException(ClazzHasException e) {
        return Result.error(e.getMessage());
    }

}
